//
//  ActionSheet.swift
//  whale
//
//  Created by topnet on 2022/3/10.
//

import UIKit

struct ActionSheetUtil {

    static func show(_ list: [String],
                     onTap: @escaping IndexClosure,
                     cancelHandler: VoidClosure? = nil,
                     bgHandler: VoidClosure? = nil) {
        let popup = PopupController()
        let v = ActionSheetView()
        v.list = list
        v.onTap = { index in
            if index == list.count {
                cancelHandler?()
                popup.dismiss(animated: false, completion: nil)
            }else {
                onTap(index)
            }
        }
       
        let popView = PopupView(containerView: popup.view, contentView: v, animationStyle: .bottom)
        popView.isDismissible = true
        popView.displayPopupView(completion: nil)
        popView.backgroundView.color = .clear
    
        popView.willDismissCallback = {
            bgHandler?()
        }
        popView.didDismissCallback = {
            popup.dismiss(animated: false, completion: nil)
        }
        popup.modalPresentationStyle = .overFullScreen
        UIApplication.shared.currentVC?.present(popup, animated: false, completion: nil)
    }

}

extension PopupController {
    
}

class ActionSheetView: UIView {
    
    var onTap: IndexClosure?
    
    var list: [String]? {
        didSet {
            
            guard let list = list, list.count > 0 else { return }
            
            for i in 0..<list.count {
                let itemV = ActionSheetItemView()
                itemV.lbl.tag = i
                itemV.text = list[i]
                itemV.lbl.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
                itemV.separatorH = i == list.count - 1 ? 5.adapter : 1.adapter
                let itemH = 50.adapter + itemV.separatorH
                itemV.frame = CGRect(x: 0, y: subviews.last?.frame.maxY ?? 0, width: UIScreen.width, height: itemH)
                addSubview(itemV)
            }
            let itemV = ActionSheetItemView()
            itemV.lbl.tag = list.count
            itemV.lbl.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
            itemV.text = "取消"
            itemV.separatorH = 0
            itemV.frame = CGRect(x: 0, y: subviews.last!.frame.maxY, width: UIScreen.width, height: 50.adapter)
            addSubview(itemV)
            
            frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: itemV.frame.maxY + UIScreen.safeBottom)
            layer.addCorner(15.adapter, color: .white, roundRect: [.topLeft, .topRight])
        }
    }
    
    @objc private func itemClick(_ sender: UIButton) {
        onTap?(sender.tag)
    }
    
}


private class ActionSheetItemView: UIView {
    var text: String? {
        didSet {
            lbl.setTitle(text, for: .normal)
        }
    }
    required convenience init() {
        self.init(frame: .zero)
        lbl = UIButton(type: .custom)
        lbl.setTitleColor(.title, for: .normal)
        lbl.titleLabel?.font = .font(15)
        addSubview(lbl)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        lbl.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height-separatorH)
        da.line(height: separatorH, color: .separator, insets: .zero)
    }
    
    var lbl: UIButton!
    var separatorH: CGFloat = 1
}
